package com.example.mypic.adapters;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import com.example.mypic.R;
import com.example.mypic.models.IPWallpaperImage;
import com.example.mypic.models.PicHelper;

import android.content.Context;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.provider.SyncStateContract.Helpers;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class PicCursorAdapter extends CursorAdapter {
//	private Context context;
	private Cursor c;
	private PicHelper helper;

	public PicCursorAdapter(Context context, Cursor c, PicHelper helper) {
		super(context, c);
		// TODO Auto-generated constructor stub
//		this.context = context;
		this.c = c;
		this.helper = helper;
	}

	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		// TODO Auto-generated method stub
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View row = inflater.inflate(R.layout.pic, parent, false);
		return row;
	}

	@Override
	public void bindView(View rowView, Context context, Cursor cursor) {
		// TODO Auto-generated method stub

		// download anh va hien thi len imageview
		try {
			ImageView ivImage = (ImageView) rowView.findViewById(R.id.ivImage);
			InputStream is;
			is = (InputStream) new URL(helper.getThumbnailImage(c))
					.getContent();

			Drawable drawable = Drawable.createFromStream(is, "iamge");
			ivImage.setImageDrawable(drawable);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		TextView tvNo = (TextView) rowView.findViewById(R.id.tvNo);
		TextView tvView = (TextView) rowView.findViewById(R.id.tvView);
		TextView tvDown = (TextView) rowView.findViewById(R.id.tvDown);
		TextView tvWidth = (TextView) rowView.findViewById(R.id.tvWidth);
		TextView tvHeight = (TextView) rowView.findViewById(R.id.tvHeight);

		tvNo.setText(helper.getID(c) + "");
		tvView.setText(helper.getView(c) + "");
		tvDown.setText(helper.getDownload(c) + "");
		tvWidth.setText(helper.getWidth(c) + "");
		tvHeight.setText(helper.getHeight(c) + "");
	}

}
